//
//  XSDiscoverSearchView.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/2.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit

class XSDiscoverSearchView: UIView {

    @IBOutlet weak var textField: UITextField!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    @IBOutlet weak var textFieldRightConstraint: NSLayoutConstraint!
    
    
    override func awakeFromNib() {
        
        leftView.frame.size = CGSizeMake(self.frame.height, frame.height)
        textField.leftView = leftView
        textField.leftViewMode = .Always
//        
//        textField.layer.borderWidth = 1
//        textField.layer.borderColor = UIColor.blackColor().CGColor
//        textField.layer.cornerRadius = 10
        
        
    }
    
    //取消按钮点击
    @IBAction func cancelBtnClick(sender: UIButton) {
        
        //1.调整文本框的约束
        textFieldRightConstraint.constant = 0
        //2.取消文本框的第一响应者
        textField.resignFirstResponder()
        
        //3.动画效果
        UIView.animateWithDuration(0.2) { () -> Void in
            self.textField.layoutIfNeeded()
        }
        
    }
    
    
    //文本框点击开始
    @IBAction func editingDidBegin(sender: UITextField) {
        
        //调整文本框的约束
        textFieldRightConstraint.constant = cancelBtn.frame.width
        
        UIView.animateWithDuration(0.2) { () -> Void in
            self.textField.layoutIfNeeded()
        }
        
    }
    
    class func discoverSearchView() -> XSDiscoverSearchView  {
        
        return NSBundle.mainBundle().loadNibNamed("XSDiscoverSearchView", owner: self, options: nil).last! as! XSDiscoverSearchView
    }
    
    lazy var leftView: UIImageView = {
        
        let leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))

        //内容模式,中心模式
        leftView.contentMode = .Center
        
        return leftView
    }()
    
    
}
